﻿using Dapper;
using OMG.Fill.Potato.Model;
using OMG.Fill.Potato.Model.Ent;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.Linq;
namespace OMG.Fill.Potato.Services
{
    /// <summary>
    /// 填报项目设置T_Set_List通用操作类
    /// </summary>
    public class SetListServices : SetLists
    {
        public Pager<List<SetListEntity>> GetPageList(SetListQuery query)
        {
            Pager<List<SetListEntity>> obj = new Pager<List<SetListEntity>>();
            var paramlist = new DynamicParameters();
            string strWhereSql = "";//from 表在下面，需完善其他查询条件在这里接的写，格式为："and xxx=@xxx空格,如下注释部分代码所示："
            if (!string.IsNullOrEmpty(query.TableCode))
            {
                strWhereSql += "and TableCode = @TableCode ";
                paramlist.Add("TableCode", query.TableCode);
            }
            if (!string.IsNullOrEmpty(query.ParentCode))
            {
                strWhereSql += "and ParentCode = @ParentCode ";
                paramlist.Add("ParentCode", query.ParentCode);
            }
            if (!string.IsNullOrEmpty(query.Status))
            {
                strWhereSql += "and Status = @Status ";
                paramlist.Add("Status", query.Status);
            }
            if (!string.IsNullOrEmpty(query.ItemCode))
            {
                strWhereSql += "and ItemCode like @ItemCode ";
                paramlist.Add("ItemCode", "%" + query.ItemCode + "%");
            }
            if (!string.IsNullOrEmpty(query.ItemName))
            {
                strWhereSql += "and ItemName like @ItemName ";
                paramlist.Add("ItemName", "%" + query.ItemName + "%");
            }
            string strFromSql = "from dbo.T_Set_List where 1=1 " + strWhereSql;
            string strTotal = "select count(1)" + strFromSql + strWhereSql;
            string strOrderSql = " order by TableCode asc,ParentCode asc,ItemCode asc";//自定义完成order by语句
            string strSql = @"select * from (select *,ROW_NUMBER() over (order by Id ) as RowNumber " + strFromSql + ") as t ";
            strSql += "where t.RowNumber between  @minRow and @maxRow" + strOrderSql;
            paramlist.Add("minRow", query.MinRow);
            paramlist.Add("maxRow", query.MaxRow);
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.rows = dbCon.Query<SetListEntity>(strSql, paramlist).ToList();
                    obj.total = dbCon.Query<int>(strTotal, paramlist).FirstOrDefault();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取填报项目设置T_Set_List分页列表失败", ex);
                return obj;
            }
        }
        public virtual ResResult<List<SortSetItem>> GetSearchList(string tableCode)
        {
            ResResult<List<SortSetItem>> obj = new ResResult<List<SortSetItem>>();
            var paramlist = new DynamicParameters();
            string strSql = @"select ParentCode,ItemName,ItemCode from dbo.T_Set_List where TableCode = @TableCode ";
            paramlist.Add("TableCode", tableCode);
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.model = dbCon.Query<SortSetItem>(strSql, paramlist).ToList();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取失败", ex);
                return obj;
            }
        }
    }
}